package com.inspur.system.service.impl;

import com.inspur.common.core.domain.AjaxResult;
import com.inspur.common.core.domain.entity.SysUser;
import com.inspur.common.utils.StringUtils;
import com.inspur.system.domain.PmPro;
import com.inspur.system.domain.vo.UserByPmId;
import com.inspur.system.domain.vo.UserByPostId;
import com.inspur.system.domain.vo.UserPostCountVo;
import com.inspur.system.dto.PmProDTO;
import com.inspur.system.mapper.PmProMapper;
import com.inspur.system.mapper.PmUserMapper;
import com.inspur.system.mapper.SysUserMapper;
import com.inspur.system.service.IPmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 【项目成员】Service业务层处理
 *
 * @date 2024-04-07
 */
@Service
public class PmUserServiceImpl implements IPmUserService {
    @Autowired
    private PmProMapper pmProMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private PmUserMapper pmUserMapper;

    @Override
    public UserByPmId countUserByPmId(PmPro pmPro) {
        PmProDTO pmPro1=pmProMapper.selectPmProByCProNo(pmPro.getcProNo());
        String[] idStrs=pmPro1.getcPrdPer().split(",");
        Long[] idLs = new Long[idStrs.length];
        for (int i=0;i<idStrs.length;i++){
            idLs[i] = Long.parseLong(idStrs[i]);
        }
        UserByPostId user=new UserByPostId();
        user.setUserIds(idLs);
        //查出人员
        List<UserByPostId> userByPostIds=userMapper.selectUserByIds(user);
        UserByPmId userByPostId=new UserByPmId();
        userByPostId.setUserPostVos(userByPostIds);
        //统计职位数量
        List<UserPostCountVo> userPostCountVos = new ArrayList<>();
        UserPostCountVo userPostCountVo1=new UserPostCountVo();
        userPostCountVo1.setPostName("项目成员");
        userPostCountVo1.setCount(idStrs.length);
        userPostCountVos.add(userPostCountVo1);
        List<UserPostCountVo> UserPostCountVos=userMapper.selectUserIdCount(idLs);
        for (UserPostCountVo vo : UserPostCountVos){
            userPostCountVos.add(vo);
        }
        userByPostId.setUserPostCounts(userPostCountVos);
        return userByPostId;
    }

    @Override
    public AjaxResult updateUserByPmId(UserByPostId userPost) {
        Long[] userIds=userPost.getUserIds();
        String idStr = new String();
        PmProDTO pmPro=pmProMapper.selectPmProByCProNo(userPost.getcProNo());
        for (int i=0;i<userIds.length;i++){
            if (pmPro.getcPrdPer().contains(userIds[i].toString())){
                return AjaxResult.error("请勿重复添加人员！");
            }
            if (i==0){
                idStr=userIds[i].toString();
            }else{
                idStr=idStr+","+userIds[i].toString();
            }
            SysUser sysUser = userMapper.selectUserById(userIds[i]);
            if (StringUtils.isNotBlank(sysUser.getProjectLink())){
                sysUser.setProjectLink(sysUser.getProjectLink()+","+userPost.getcProNo());
            }else {
                sysUser.setProjectLink(userPost.getcProNo());
            }
            userMapper.updateUser(sysUser);
        }
        if(StringUtils.isNotBlank(pmPro.getcPrdPer())){
            idStr=pmPro.getcPrdPer()+","+idStr;
        }
        PmPro pmPro1 =new PmPro();
        pmPro1.setcProNo(userPost.getcProNo());
        pmPro1.setcPrdPer(idStr);
        pmPro1.setcPerNum(Long.parseLong(userIds.length+pmPro.getcPerNum()+""));
        pmProMapper.updatePmPro(pmPro1);
        return AjaxResult.success();
    }

    @Override
    public List<UserByPostId> UserListByPostId(UserByPostId userPost) {
        PmProDTO pmPro1=pmProMapper.selectPmProByCProNo(userPost.getcProNo());
        String[] idStrs=pmPro1.getcPrdPer().split(",");
        Long[] idLs = new Long[idStrs.length];
        for (int i=0;i<idStrs.length;i++){
            idLs[i] = Long.parseLong(idStrs[i]);
        }
        userPost.setUserIds(idLs);
        List<UserByPostId> UserListByPostId = userMapper.UserListByPostId(userPost);
        return UserListByPostId;
    }

    @Override
    public int deleteUserByPmId(String[] userIds, String cProNo) {
        //查出项目中的人员
        PmProDTO pmPro1=pmProMapper.selectPmProByCProNo(cProNo);
        String userid=new String();
        for (int t=0;t<userIds.length;t++){
            if (t==0){
                userid=userIds[t];
            }else if (t<userIds.length){
                userid=userid+","+userIds[t];
            }
            SysUser sysUser=userMapper.selectUserById(Long.parseLong(userIds[t]));
            //保存删除后对的我关联的项目
            if (sysUser.getProjectLink().contains(cProNo)){
                String[] projectLinks = sysUser.getProjectLink().split(",");
                String[] projectLinksNew = new String[projectLinks.length];
                String projectLinkNew = new String();
                int countP=0;
                for(int i=0;i<projectLinks.length;i++){
                    if (!cProNo.equals(projectLinks[i])){
                        projectLinksNew[countP]=projectLinks[i];
                        countP++;
                    }
                }
                for (int i=0;i<projectLinksNew.length;i++){
                    if (StringUtils.isNotBlank(projectLinksNew[i])){
                        if (i==0){
                            projectLinkNew=projectLinksNew[i];
                        }else if (t<projectLinksNew.length){
                            projectLinkNew=projectLinkNew+","+projectLinksNew[i];
                        }
                    }
                }
                sysUser.setProjectLink(projectLinkNew);
            }
            //保存删除后的我创建的项目
            if (StringUtils.isNotBlank(sysUser.getCreateProject())&&sysUser.getCreateProject().contains(cProNo)){
                String[] creatPros = sysUser.getCreateProject().split(",");
                String[] creatProsNew = new String[creatPros.length];
                String creatPro = new String();
                int countP=0;
                for(int i=0;i<creatPros.length;i++){
                    if (!cProNo.equals(creatPros[i])){
                        creatProsNew[countP]=creatPros[i];
                        countP++;
                    }
                }
                for (int i=0;i<creatProsNew.length;i++){
                    if (StringUtils.isNotBlank(creatProsNew[i])){
                        if (i==0){
                            creatPro=creatProsNew[i];
                        }else if (t<creatProsNew.length){
                            creatPro=creatPro+","+creatProsNew[i];
                        }
                    }

                }
                sysUser.setCreateProject(creatPro);
            }

            //保存删除后的我负责的项目
            if (StringUtils.isNotBlank(sysUser.getHeadProject())&&sysUser.getHeadProject().contains(cProNo)){
                String[] headProjects = sysUser.getHeadProject().split(",");
                String[] headProjectsNew = new String[headProjects.length];
                String headProjectNew = new String();
                int countP=0;
                for(int i=0;i<headProjects.length;i++){
                    if (!cProNo.equals(headProjects[i])){
                        headProjectsNew[countP]=headProjects[i];
                        countP++;
                    }
                }
                for (int i=0;i<headProjectsNew.length;i++){
                    if (StringUtils.isNotBlank(headProjectsNew[i])){
                        if (i==0){
                            headProjectNew=headProjectsNew[i];
                        }else if (t<headProjectsNew.length){
                            headProjectNew=headProjectNew+","+headProjectsNew[i];
                        }
                    }

                }
                sysUser.setHeadProject(headProjectNew);
            }
            userMapper.updateUser(sysUser);
        }

        String[] idStrs=pmPro1.getcPrdPer().split(",");
        String[] userIdsNew=new String[idStrs.length];
        int count=0;
        for (int t=0;t<idStrs.length;t++){
            if (!userid.contains(idStrs[t])){
                userIdsNew[count]=idStrs[t];
                count++;
            }
        }
        String userIdNew=new String();
        for (int t=0;t<userIdsNew.length;t++){
            if (StringUtils.isNotBlank(userIdsNew[t])){
                if (t==0){
                    userIdNew=userIdsNew[t];
                }else if (t<userIdsNew.length){
                    userIdNew=userIdNew+","+userIdsNew[t];
                }
            }

        }
        //根据项目编号保存删除后的成员；
        PmPro pmPro=new PmPro();
        pmPro.setcProNo(cProNo);
        pmPro.setcPrdPer(userIdNew);
        pmPro.setcPerNum(pmPro1.getcPerNum()-userIds.length);
        pmProMapper.updatePmPro(pmPro);
        return pmProMapper.updatePmPro(pmPro);
    }

    @Override
    public List<UserByPostId> selectPmProUserListExport(UserByPostId userPost) {
        List<UserByPostId> sysUsers = pmUserMapper.selectPmProListExport(userPost);
        return sysUsers;
    }
}
